#include "CS_Screen.h"
#include "CS_Global.h"
#include "CS_Logic.h"

#include <stdio.h>
#include <string.h>

unsigned char getCenterX(const char* string)
{
	return (SCREEN_WIDTH / 2u) - (strlen(string) / 2u);
}

unsigned char getCenterY()
{
	return (SCREEN_HEIGHT / 2u);
}

void fillPlayField(unsigned char x, unsigned char y)
{
	(void)textcolor(COLOR_BLACK);
	cputcxy(x + 1u, y + 1u, gameBoard[0]);
	cputcxy(x + 3u, y + 3u, gameBoard[5]);
	cputcxy(x + 5u, y + 5u, gameBoard[10]);
	cputcxy(x + 7u, y + 7u, gameBoard[15]);

	(void)textcolor(COLOR_RED);
	cputcxy(x + 3u, y + 1u, gameBoard[1]);
	cputcxy(x + 5u, y + 3u, gameBoard[6]);
	cputcxy(x + 7u, y + 5u, gameBoard[11]);
	cputcxy(x + 1u, y + 7u, gameBoard[12]);

	(void)textcolor(COLOR_GREEN);
	cputcxy(x + 5u, y + 1u, gameBoard[2]);
	cputcxy(x + 7u, y + 3u, gameBoard[7]);
	cputcxy(x + 1u, y + 5u, gameBoard[8]);
	cputcxy(x + 3u, y + 7u, gameBoard[13]);

	(void)textcolor(COLOR_BLUE);
	cputcxy(x + 7u, y + 1u, gameBoard[3]);
	cputcxy(x + 1u, y + 3u, gameBoard[4]);
	cputcxy(x + 3u, y + 5u, gameBoard[9]);
	cputcxy(x + 5u, y + 7u, gameBoard[14]);
}

void drawPlayfield(unsigned char x, unsigned char y)
{
	const char topLine[] = { CH_ULCORNER, CH_HLINE, CH_TTEE, CH_HLINE, CH_TTEE, CH_HLINE, CH_TTEE, CH_HLINE, CH_URCORNER, 0x00 };
	const char insideLine[] = { CH_VLINE, CH_SPACE, CH_VLINE, CH_SPACE, CH_VLINE, CH_SPACE, CH_VLINE, CH_SPACE, CH_VLINE, 0x00 };
	const char middleLine[] = { CH_LTEE, CH_HLINE, CH_CROSS, CH_HLINE, CH_CROSS, CH_HLINE, CH_CROSS, CH_HLINE, CH_RTEE, 0x00 };
	const char bottomLine[] = { CH_LLCORNER, CH_HLINE, CH_BTEE, CH_HLINE, CH_BTEE, CH_HLINE, CH_BTEE, CH_HLINE, CH_LRCORNER, 0x00 };
	unsigned char oldColor = textcolor(COLOR_YELLOW);

	cputsxy(x, y, topLine);
	cputsxy(x, y + 1u, insideLine);
	cputsxy(x, y + 2u, middleLine);
	cputsxy(x, y + 3u, insideLine);
	cputsxy(x, y + 4u, middleLine);
	cputsxy(x, y + 5u, insideLine);
	cputsxy(x, y + 6u, middleLine);
	cputsxy(x, y + 7u, insideLine);
	cputsxy(x, y + 8u, bottomLine);

	cputcxy(x + 12u, y, '1');
	cputsxy(x + 13u, y + 1u, bottomLine);

	if(numberOfPlayers >= 2)
	{
		cputcxy(x + 12u, y + 4u, '2');
		cputsxy(x + 13u, y + 5u, bottomLine);
	}

	if(numberOfPlayers >= 3)
	{
		cputcxy(x + 12u, y + 8u, '3');
		cputsxy(x + 13u, y + 9u, bottomLine);
	}

	if(numberOfPlayers == 4)
	{
		cputcxy(x + 12u, y + 12u, '4');
		cputsxy(x + 13u, y + 13u, bottomLine);
	}

	fillPlayField(x, y);

	(void)textcolor(COLOR_RED);
	cputsxy(SCREEN_LEFT, 19, "  Roll dice to fill");
	cputsxy(SCREEN_LEFT, 20, "   your tray with");
	cputsxy(SCREEN_LEFT, 21, "  one of each card.");
	(void)textcolor(COLOR_BLUE);
	gotoxy(SCREEN_LEFT, 22); cprintf("    F2: %d Players", numberOfPlayers);
	(void)textcolor(oldColor);

	return;
}

void displayPlayerTray(unsigned char x, unsigned char y, unsigned char player)
{
	unsigned char xx, yy, counter, value, color;
	yy = y + 4 * (player);
	xx = x + 14;
	
	for(counter = 0; counter < 4; ++counter)
	{
		value = playerTrays[player][counter];
		color = value >> 4;
		//gotoxy(counter * 4, 21); cprintf("%X", value & 0x0F);

		(void)textcolor(color);
		switch(value & 0x0F)
		{
			case 1:	cputcxy(xx + (2u * counter), yy, DIE_SIDES[0]);	break;
			case 2:	cputcxy(xx + (2u * counter), yy, DIE_SIDES[1]);	break;
			case 4:	cputcxy(xx + (2u * counter), yy, DIE_SIDES[2]);	break;
			case 8:	cputcxy(xx + (2u * counter), yy, DIE_SIDES[3]);	break;
		}
	}

	gotoxy(xx-1u, yy + 2u); cprintf("Score: %2d", calculateScore(player));
}

void waitForNextPlayer(unsigned char x, unsigned char y)
{
	unsigned char message[] = "PLAYER %d PRESS RETURN";
	unsigned char key = 0;

	(void)sprintf(message, message, currentPlayer + 1);

	cputsxy(getCenterX(message), y - 2, message);

	while(key != CH_ENTER)
	{
		key = cgetc();
	}

	cclearxy(getCenterX(message), y - 2, strlen(message));
}

#define URL "cardswipe.codeplex.com"
#define BY  "Game By: Payton Byrd"
void displayCredits(unsigned char x, unsigned char y)
{
	unsigned delayCounter = 0;

	(void)textcolor(COLOR_BLUE);
	cputsxy(getCenterX(URL), y, URL);
	(void)textcolor(COLOR_GREEN);
	cputsxy(getCenterX(BY), y + 2, BY);

	for(;delayCounter < 10000; ++delayCounter);
}